<template>
  <div>
    <el-card class="box-card" style="margin-bottom:10px;position:relative;">
      <div slot="header" class="clearfix">
        <span>任务列表</span>
        <el-button style="float: right;" size="mini" @click="refreshTasks">刷新</el-button>
        <el-button style="float: right;margin-right:10px;" size="mini" @click="acrionTaskEditorOpen">新建</el-button>
      </div>
      
      <el-table :data="tasks" style="width: 100%">
        <el-table-column prop="timeReadable" label="时间" width="200"></el-table-column>
        <el-table-column prop="cmd" label="命令"></el-table-column>
        <el-table-column prop="comment" label="备注" width="180"></el-table-column>
        <el-table-column align="right">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="taskDelete(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <task-edit ref="taskEditor"></task-edit>
  </div>
</template>
<script>
import CrontabParser from './CrontabParser.js'
import TaskEdit from './TaskEdit.vue'
export default {
    name : 'CrontabMain',
    components : {
        'task-edit' : TaskEdit,
    },
    data() {
        return {
            tasks : [],
            user : null,
            client : null,
        }
    },
    /** 页面挂载完成 */
    async mounted () {
        let server = this.$store.getters.serverCurrent;
        this.client = await server.getSshClient();
        this.user = server.username;
        await this.refreshTasks();
    },
    methods : {
        /** 打开编辑框 */
        async acrionTaskEditorOpen() {
            this.$refs.taskEditor.client = this.client;
            let newTask = await this.$refs.taskEditor.open();
            this.tasks.push(newTask);
            await this.crontabSync();
        },

        /** 刷新任务 */
        async refreshTasks () {
            let output = await this.client.exec(`crontab -u ${this.user} -l`);
            let parser = new CrontabParser(output);
            this.tasks = parser.parse();
        },

        /** 删除任务 */
        async taskDelete ( index ) {
            try {
                await this.$confirm('此操作将永久删除该任务, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                });
            } catch {
                return;
            }

            this.tasks.splice(index, 1);
            await this.crontabSync();
        },

        /** 同步任务 */
        async crontabSync() {
            let content = [];
            for ( let i=0; i<this.tasks.length; i++ ) {
                if ( null != this.tasks[i].comment ) {
                    content.push('# '+this.tasks[i].comment);
                }
                content.push(this.tasks[i].defination);
            }
            content = content.join('\n') + "\n";

            let server = this.$store.getters.serverCurrent;
            let tmpFilePath = await server.exec('mktemp');
            tmpFilePath = tmpFilePath.trim();
            await server.filePutContent(tmpFilePath, content);
            await server.exec(`crontab -u ${this.user} -r`);
            await server.exec(`crontab -u ${this.user} ${tmpFilePath}`);
            await server.exec(`rm -f ${tmpFilePath}`);
            await this.refreshTasks();
        }
    }
}
</script>